extends NodeState

@export var character:Villager
@export var animated_sprite_2d:AnimatedSprite2D
@export var navigation_agent_2d: NavigationAgent2D
@export var min_speed: float = 15.0
@export var max_speed: float = 20.0
var npc_name:String
var speed: float

func _ready() -> void:
	navigation_agent_2d.velocity_computed.connect(on_safe_velocity_computed)
	call_deferred("character_setup")
func character_setup() -> void:
	await get_tree().physics_frame
	#if !character.motionless:
		#set_movement_target()

func set_movement_target() -> void:
	var target_position: Vector2 = NavigationServer2D.map_get_random_point(navigation_agent_2d.get_navigation_map(), navigation_agent_2d.navigation_layers, false)
	var target_grid = GridUtils.world_to_grid(target_position)
	var current_grid = GridUtils.world_to_grid(character.position)
	var go:Vector2i = target_grid - current_grid
	print_debug("target_grid:",target_grid," current_grid:",current_grid," go",go)
	var displacement = Vector2iUtils.project(go,go.abs().max_axis_index())
	var direction = displacement.sign()
	_play_animation(direction)
	
	target_position = GridUtils.grid_to_world(current_grid + Vector2iUtils.project(go,go.abs().max_axis_index()))
	navigation_agent_2d.target_position = target_position
	speed = randf_range(min_speed, max_speed)
	
func _on_process(_delta : float) -> void:
	pass


func _on_physics_process(_delta : float) -> void:
	if navigation_agent_2d.is_navigation_finished():
		character.current_move_cycle += 1
		set_movement_target()
		return
	
	var target_position: Vector2 = navigation_agent_2d.get_next_path_position()
	var target_direction: Vector2 = character.global_position.direction_to(target_position)
	var velocity: Vector2 = target_direction * speed
	
	if navigation_agent_2d.avoidance_enabled:
		
		navigation_agent_2d.velocity = velocity
	else:
		character.velocity = velocity
		var is_collision = character.move_and_slide()
		# 遇到障碍改变路线
		if is_collision:
			print_debug("is_collision and change way")
			character.current_move_cycle += 1
			set_movement_target()

func _play_animation(direction:Vector2i) -> void:
	if direction==Vector2i.UP:
		animated_sprite_2d.play("move_up")
		print_debug("向上走")
	elif direction==Vector2i.DOWN:
		animated_sprite_2d.play("move_down")
		print_debug("向下走")
	elif direction==Vector2i.LEFT:
		animated_sprite_2d.play("move_left")
		print_debug("向左走")
	elif direction==Vector2i.RIGHT:
		animated_sprite_2d.play("move_right")
		print_debug("向右走")
	else:
		print_debug("没有方向，非常见鬼：direction：",direction)

func on_safe_velocity_computed(safe_velocity: Vector2) -> void:
	#animated_sprite_2d.flip_h = safe_velocity.x < 0
	character.velocity = safe_velocity
	character.move_and_slide()

func _on_next_transitions() -> void:
	if character.current_move_cycle == character.move_cycles:
		character.velocity = Vector2.ZERO
		transition.emit("Idle")


func _on_enter() -> void:
	#animated_sprite_2d.play("move_down")
	character.current_move_cycle = 0
	set_movement_target()

func _on_exit() -> void:
	animated_sprite_2d.stop()
